package com.yoga.service.impl;

import com.yoga.entity.*;
import com.yoga.mapper.*;
import com.yoga.service.VenueService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class VenueServiceImpl implements VenueService {

    @Resource
    private YogaVenueDao yogaVenueDao;
    @Resource
    private YogaCoachDao yogaCoachDao;
    @Resource
    private YogaUserDao yogaUserDao;
    @Resource
    private CoachPrivateDao coachPrivateDao;
    @Resource
    private YogaPrivaterequestDao yogaPrivaterequestDao;
    @Override
    public List<YogaCoach> findAllCoach(Integer vid) {
        YogaCoachExample example = new YogaCoachExample();
        example.createCriteria().andVidEqualTo(vid);
        List<YogaCoach> coachList = yogaCoachDao.selectByExample(example);
        return coachList;
    }
    @Override
    public List<CoachPrivate> findAllPrivate() {
        return coachPrivateDao.selectByExample(null);
    }

    @Override
    public Boolean addCoach(YogaCoach yogaCoach) {
        int i = yogaCoachDao.insert(yogaCoach);
        return i>0?true:false;
    }

    @Override
    public List<YogaVenue> findAllVenue() {
        List<YogaVenue> list = yogaVenueDao.selectByExample(null);
        return list;
    }

    @Override
    public YogaVenue findOneVenue(String address) {
        YogaVenueExample example = new YogaVenueExample();
        example.createCriteria().andAddressLike(address);
        List<YogaVenue> list = yogaVenueDao.selectByExample(example);
        return list.size()>0? list.get(0):null;
    }

    @Override
    public List<YogaVenue> findSomeVenue(String address) {
        YogaVenueExample example = new YogaVenueExample();
        example.createCriteria().andAddressLike(address);
        List<YogaVenue> list = yogaVenueDao.selectByExample(example);
        return list;
    }

    @Override
    public List<YogaUser> findAllUser() {
        List<YogaUser> userList = yogaUserDao.selectByExample(null);
        return userList;
    }

    @Override
    public YogaVenue findVenueByVid(Integer vid) {
        YogaVenue yogaVenue = yogaVenueDao.selectByPrimaryKey(vid);
        return yogaVenue;
    }

    @Override
    public YogaUser login(YogaUser yogaUser) {
        YogaUserExample example = new YogaUserExample();
        YogaUserExample.Criteria criteria = example.createCriteria();
        criteria.andPhoneEqualTo(yogaUser.getPhone());
        criteria.andUpwdEqualTo(yogaUser.getUpwd());
        List<YogaUser> yogaUserinfoList = yogaUserDao.selectByExample(example);
        return yogaUserinfoList.size()==0?null:yogaUserinfoList.get(0);
    }

    @Override
    public YogaUser logins(YogaUser yogaUser) {
        YogaUserExample example = new YogaUserExample();
        YogaUserExample.Criteria criteria = example.createCriteria();
        criteria.andPhoneEqualTo(yogaUser.getPhone());
        List<YogaUser> yogaUserList = yogaUserDao.selectByExample(example);
        return yogaUserList.size()==0?null:yogaUserList.get(0);
    }

    @Override
    public Boolean addVenue(YogaVenue yogaVenue) {
        int i = yogaVenueDao.insert(yogaVenue);
        return i>0?true:false;
    }

    @Override
    public Boolean activeVenue(YogaVenue yogaVenue) {
        int i = yogaVenueDao.updateByPrimaryKeySelective(yogaVenue);
        return i>0?true:false;
    }

    @Override
    public Boolean deleteCoach(Integer uid) {
        YogaUser user = yogaUserDao.selectByPrimaryKey(uid);
        user.setAge(1337);
        int i = yogaUserDao.updateByPrimaryKeySelective(user);
        return i>0?true:false;
    }

    @Override
    public Boolean changeVenueUser(YogaVenue yogaVenue, YogaUser yogaUser) {
        int j = yogaUserDao.updateByPrimaryKeySelective(yogaUser);
        int i = yogaVenueDao.updateByPrimaryKeySelective(yogaVenue);
        return i>0?true:false;
    }

    @Override
    public Boolean resetUserPwd(Integer uid) {
        YogaUser user = yogaUserDao.selectByPrimaryKey(uid);
        user.setUpwd("000000");
        int i = yogaUserDao.updateByPrimaryKeySelective(user);
        return i>0?true:false;
    }

    @Override
    public List<YogaPrivaterequest> findAllPrivaterequest() {
        List<YogaPrivaterequest>list = yogaPrivaterequestDao.selectByExample(null);
        return list;
    }

    @Override
    public YogaVenue getVenueByUid(Integer uid) {
        YogaCoach coach = yogaCoachDao.selectByPrimaryKey(uid);
        Integer vid = coach.getVid();
        YogaVenue yogaVenue = yogaVenueDao.selectByPrimaryKey(vid);
        return yogaVenue;
    }


}
